<?php namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Watson\Validating\ValidatingTrait;
use Overtrue\Pinyin\Pinyin;
use Illuminate\Database\Eloquent\Model;

/**
 * 学校
 * Class School
 *
 * @package App\Models
 * @property int $id
 * @property string $name 名称
 * @property int $province 省份id
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 * @method static \Illuminate\Database\Query\Builder|\App\Models\School whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\School whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\School whereProvinceId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\School whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\School whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\School whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\School whereLetter($value)
 * @mixin \Eloquent
 */
class School extends Model
{
    use SoftDeletes;
    use ValidatingTrait;

    const CACHE_TAG = 'school';
    
    protected $table = 'xyj_schools';

    protected $dates = ['deleted_at'];

    protected $fillable = ['name', 'province', 'city','district','bosshead'];

    protected $rules = [
        'name'        => 'required|unique:xyj_schools,name,NULL,id,level,1',
        'province' => 'required',
        'city' => 'required',
      //  'district' => 'required'
        
        
    ];

    protected $validationMessages = [
        'name.required'        => '名称必填',
        'name.unique'          => '名称重复',
        'province.required' => '省份必填',
        'city.required' => '市区必填',
        'district.required' => '县/街道必填',
        
        
    ];

    protected $injectUniqueIdentifier = true;

    public function province()
    {
        return $this->belongsTo('\App\Models\District', 'province');
    }
    
    public function city()
    {
        return $this->belongsTo('\App\Models\District', 'city');
    }
    
    public function district()
    {
        return $this->belongsTo('\App\Models\District', 'district');
    }
    
    public function students()
    {
        return $this->hasMany('App\Models\Student');
    }

    /**
     * 用于select的列表
     * @return array
     * @deprecated
     */
    public static function getListData($provinceId)
    {
        return \Cache::tags(self::CACHE_TAG)->rememberForever('schools_' . $provinceId, function () use ($provinceId) {
            $infos = self::where('province', '=', $provinceId)->orderBy('id', 'desc')->get();
            $listData = ['0' => '请选择'];
            foreach ($infos as $info) {
                $listData[$info->id] = $info->name;
            }
            return $listData;
        });
    }

    /**
     * 生成名称的拼音
     
    public function generateLetter()
    {
        $this->letter = Pinyin::letter($this->name, ['delimiter' => '']);
    }
    */

    /**
     * 用于select的列表
     * @param $provinceId
     * @return mixed
     *
     * @deprecated
     */
    public static function getListDataJson($provinceId)
    {
        return \Cache::tags(self::CACHE_TAG)->rememberForever('schools_json_' . $provinceId, function () use ($provinceId) {
            return self::where('province_id', '=', $provinceId)->orderBy('letter', 'asc')->select(['id', 'name'])->get()->toJson();
        });
    }
    
    
    /**
     * 用于select的学校列表
     * @return array
     */
    public static function getSchoolList( $cityId )
    {
        $result = \Cache::tags(self::CACHE_TAG)->rememberForever('new-schools-list:' . $cityId, function () use ($cityId) {
            $infos = self::where('city','=',$cityId)->where('level',1)->select(['id','name'])->get()->toArray();
            return json_encode($infos, JSON_UNESCAPED_UNICODE );
        });
        return json_decode($result, true);
    }

    /**
     * 清除缓存
     */
    public function clearCache()
    {
        \Cache::tags(self::CACHE_TAG)->flush();
    }

    public static function getGgrSchoolList()
    {
        $resultList = [];
        $schoolList = self::where('level' , 1)->get();
        foreach ($schoolList as $school){
            $resultList[$school->id] = $school->name;
        }
        return $resultList;
    }

}